package com.ghxxzx.daglcustom.business_fk.file.api;

import java.io.File;
import java.io.FileInputStream;
import java.net.URLEncoder;

import javax.servlet.ServletOutputStream;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.tmsps.ne4spring.base.BaseAction;
import org.tmsps.ne4spring.utils.ChkUtil;
import org.tmsps.ne4spring.utils.img.ImgTools;

import com.ghxxzx.daglcustom.business_fk.file.domain.FilePo;
import com.ghxxzx.daglcustom.utils.WebUtil;

/**
 * 实现文件管理
 * 
 * @author 刘华
 *
 */
@RestController
public class FileFeginApiAction extends BaseAction {

	@Value("${app.filepath}")
	private String filepath;

	/**
	 * .下载文件
	 * 
	 * @param fileId
	 * @throws Exception
	 */
	@GetMapping("/download/{fileId}")
	public void download(@PathVariable String fileId) throws Exception {

		FilePo file = bs.findById(fileId, FilePo.class);
		String DATA_PATH = filepath;
		ServletOutputStream os = WebUtil.getReponse().getOutputStream();
		FileInputStream fis = null;
		try {
			File img = null;
			if (file != null) {
				img = new File(DATA_PATH + File.separator + file.getFolder(), file.getNew_file_name());
			}
			if (img == null || !img.exists()) {
				img = new File(WebUtil.getRequest().getSession().getServletContext().getRealPath("") + File.separator
						+ "resource" + File.separator + "img", "file404.jpg");
			}
			// 设置页面不缓存
			WebUtil.getReponse().setHeader("Pragma", "No-cache");
			WebUtil.getReponse().setHeader("Cache-Control", "no-cache");
			WebUtil.getReponse().setDateHeader("Expires", 0);
			WebUtil.getReponse().setHeader("content-disposition",
					"attachment;filename=" + URLEncoder.encode(file.getFile_name(), "UTF-8"));

			fis = new FileInputStream(img);
			byte[] b = new byte[10240];
			int len = -1;
			while ((len = fis.read(b)) != -1) {
				os.write(b, 0, len);
			}

		} catch (Exception e) {
			System.err.println("文件找不到:" + e);
		} finally {
			os.close();
			if (fis != null) {
				fis.close();
			}
		}
	}

	/**
	 * .查看图片
	 * 
	 * @param fileId
	 * @param mw
	 * @param mh
	 * @throws Exception
	 */
	@GetMapping("/img/{fileId}")
	public void img(@PathVariable String fileId, String mw, String mh) throws Exception {
		if (ChkUtil.isNull(fileId)) {
			return;
		}

		FilePo file = bs.findById(fileId, FilePo.class);
		ServletOutputStream os = WebUtil.getReponse().getOutputStream();
		// 获取保存路径
		String DATA_PATH = filepath;
		try {
			File img = null;
			if (file != null) {
				img = new File(DATA_PATH + File.separator + file.getFolder() + File.separator + file.getNew_file_name());
			}
			if (img == null || !img.exists()) {
				img = new File(WebUtil.getRequest().getSession().getServletContext().getRealPath("") + File.separator
						+ "resource" + File.separator + "img", "file404.jpg");
			}

			int height = ChkUtil.getInteger(mh);
			int width = ChkUtil.getInteger(mw);
			height = height < 0 ? 0 : height;
			width = width < 0 ? 0 : width;

			// 设置页面不缓存
			WebUtil.getReponse().setHeader("Pragma", "No-cache");
			WebUtil.getReponse().setHeader("Cache-Control", "no-cache");
			WebUtil.getReponse().setDateHeader("Expires", 0);

			ImgTools.thumbnail_w_h(img, width, height, os);

		} catch (Exception e) {
			System.err.println("图片找不到:" + e);
		} finally {
			os.close();
		}
	}

}
